package com.probox.modules.controller.product;

import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.product.ProductTags;
import com.probox.common.entity.domain.product.vo.ProductInfoTagsVo;
import com.probox.common.entity.service.product.IProductInfoTagsService;
import com.probox.common.entity.service.product.IProductTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品标签信息Controller
 *
 *
 * @date 2021-05-27
 */
@RestController
@RequestMapping("/product/tags")
public class ProductTagsController extends BaseController {
    @Autowired
    private IProductTagsService productTagsService;

    @Autowired
    private IProductInfoTagsService productInfoTagsService;

    /**
     * 查询商品标签信息列表
     */
    @PreAuthorize(hasPermi = "product:tags:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductTags productTags) {
        startPage();
        List<ProductTags> list = productTagsService.queryList(productTags);
        return getDataTable(list);
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/bulidSelect")
    public AjaxResult bulidSelect() {
        return AjaxResult.success(productTagsService.bulidSelect());
    }

    /**
     * 导出商品标签信息列表
     */
    @PreAuthorize(hasPermi = "product:tags:export")
    @Log(title = "标签信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductTags productTags) throws IOException {
        List<ProductTags> list = productTagsService.queryList(productTags);
        ExcelUtil<ProductTags> util = new ExcelUtil<ProductTags>(ProductTags.class);
        util.exportExcel(response, list, "标签信息数据");
    }

    /**
     * 获取商品标签信息详细信息
     */
    @PreAuthorize(hasPermi = "product:tags:query")
    @GetMapping(value = "/{tagsUid}")
    public AjaxResult getInfo(@PathVariable("tagsUid") Long tagsUid) {
        return AjaxResult.success(productTagsService.selectProductTagsById(tagsUid));
    }

    /**
     * 新增商品标签信息
     */
    @PreAuthorize(hasPermi = "product:tags:add")
    @Log(title = "标签信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductTags productTags) {
        return toAjax(productTagsService.insertProductTags(productTags));
    }

    /**
     * 修改商品标签信息
     */
    @PreAuthorize(hasPermi = "product:tags:edit")
    @Log(title = "标签信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductTags productTags) {
        return toAjax(productTagsService.updateProductTags(productTags));
    }

    /**
     * 删除商品标签信息
     */
    @PreAuthorize(hasPermi = "product:tags:remove")
    @Log(title = "标签信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagsUids}")
    public AjaxResult remove(@PathVariable Long[] tagsUids) {
        return toAjax(productTagsService.deleteProductTagsByIds(tagsUids));
    }

    /**
     * 获取标签相关的商品信息
     */
    @GetMapping(value = "/productList/{tagsUid}")
    public AjaxResult getTagsProduct(@PathVariable("tagsUid") Long tagsUid) {
        return AjaxResult.success(productInfoTagsService.geTagsList(tagsUid));
    }


    @PutMapping("/productList")
    public AjaxResult updateTagsList(@RequestBody ProductInfoTagsVo vo) {
        productInfoTagsService.updateTagsList(vo);
        return AjaxResult.success();
    }
}
